package org.jeik.beackBeans.admin;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.jeik.beackBeans.BeansBase;
import org.jeik.beackBeans.contents.PageContentsBean;
import org.jeik.common.Constants;
import org.jeik.controllers.MsgsController;
import org.jeik.controllers.NavigationController;
import org.jeik.controllers.NavigationRule;
import org.jeik.data.dao.DataException;
import org.jeik.data.models.Page;
import org.jeik.data.services.PageContentsService;
import org.jeik.utils.Configs;
import org.jeik.utils.WebUtils;
import org.primefaces.event.FileUploadEvent;

/**
 * 
 * @author AAsha
 * 
 */
public class ContentsHandlerBean extends BeansBase {

	//Constants
	private static final long serialVersionUID = 1L;
	private static final String PAGE_ID = "PAGE_ID";
	private static final String PAGE_NAV = "PAGE_NAV";
	
	//Fields
	private Page page = new Page();
	
	private List<File> uploadedFiles = new ArrayList<File>();
	
	/**
	 * 
	 */
	public ContentsHandlerBean() {
	}
	
	/**
	 * 
	 */
	public void loadPage(){
		
		String pageID = WebUtils.getRequest().getParameter(PAGE_ID);
		String pageNav = WebUtils.getRequest().getParameter(PAGE_NAV);
		
		PageContentsBean pageContentsBean = (PageContentsBean) WebUtils
				.resolveExpression(WebUtils.PAGE_CONTENTS_BEAN,
						PageContentsBean.class);
		page = new Page(pageContentsBean.getContents().get(pageID));
		
		NavigationController.navigate(NavigationRule.valueOf(pageNav));
		
	}
	
	/**
	 * 
	 * @throws DataException
	 */
	public void updatePage() throws DataException{
		
		PageContentsService.getInstance().updatePage(page);

		PageContentsBean pageContentsBean = (PageContentsBean) WebUtils.resolveExpression(WebUtils.PAGE_CONTENTS_BEAN, PageContentsBean.class);
		pageContentsBean.getContents().put(page.getId(), page);
		
		//Realocate File from temp directory.
		if(uploadedFiles != null && uploadedFiles.size() > 0){
			File newfile = uploadedFiles.get(0);
			ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();  
	        String dir = servletContext.getRealPath("") + File.separator + "resources" + File.separator + "images";  
			
	        File oldFile = new File(dir, newfile.getName());
	        if(oldFile.exists()){
	        	oldFile.delete();
	        }
	        
	        newfile.renameTo(new File(dir, newfile.getName()));
			uploadedFiles.clear();
		}
		
		MsgsController.addInfoMessage("");
		
		NavigationController.navigate(NavigationRule.HOME);
	}
	
	/**
	 * 
	 * @param event
	 */
	public void handleAmbassadorImageUpload(FileUploadEvent event) {  
		
		File uploadedFile = null;
		
		try {
			
            File filesDir = new File(Configs.getConfigValue(Constants.Configs.TEMP_FILES_DIR));
            
            uploadedFile = new File(filesDir, "Ambassador.gif");
            
            InputStream inputStream = event.getFile().getInputstream();
            OutputStream out = new FileOutputStream(uploadedFile);
            
            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = inputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            
            inputStream.close();
            out.flush();
            out.close();
            
            uploadedFiles.clear();
            uploadedFiles.add(uploadedFile);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        
		MsgsController.addInfoMessage("");
    }
	
	//==========================================
	// Setters and Getters
	//==========================================
	
	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
}
